package com.hh.admin.controller;

import com.hh.common.model.pagemanagement.Banner;
import com.hh.common.request.pagemanagement.BannerSaveRequest;
import com.hh.common.response.CommonResult;
import com.hh.common.response.pagemanagement.BannerResponse;
import com.hh.common.response.pagemanagement.BannerTabsHeader;
import com.hh.common.vo.JingangAreaVo;
import com.hh.service.service.pagemanagement.BannerService;
import com.hh.service.service.pagemanagement.JingangAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("api/admin/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private JingangAreaService jingangAreaService;

    /**
     * 轮播图tabs表头数据
     */
    @RequestMapping(value = "/tabs/headers", method = RequestMethod.GET)
    public CommonResult<List<BannerTabsHeader>> getTabsHeader() {
        return CommonResult.success(this.bannerService.getTabsHeader());
    }

    /**
     * 轮播图列表
     *
     * @param position 搜索条件
     */
    @PreAuthorize("hasAuthority('admin:banner:list')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<BannerResponse>> getList(@RequestParam("position") Integer position) {
        return CommonResult.success(this.bannerService.getAdminList(position));
    }

    /**
     * 轮播图详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<Banner> info(@RequestParam("id") Integer id) {
        return CommonResult.success(this.bannerService.getInfo(id));
    }


    /**
     * 保存banner图
     *
     * @param request
     * @return
     */
    @PreAuthorize("hasAuthority('admin:banner:save')")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<Boolean> save(@Validated @RequestBody BannerSaveRequest request) {
        try {
            return CommonResult.success(this.bannerService.save(request));
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<Boolean> update(@Validated @RequestBody BannerSaveRequest request) {
        try {
            return CommonResult.success(this.bannerService.update(request));
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }

    }

    /**
     * 显示
     */
    @PreAuthorize("hasAuthority('admin:banner:display')")
    @RequestMapping(value = "/display/{id}", method = RequestMethod.GET)
    public CommonResult<String> putOn(@PathVariable Integer id) {
        if (this.bannerService.display(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 隐藏
     */
    @PreAuthorize("hasAuthority('admin:banner:display')")
    @RequestMapping(value = "/hide/{id}", method = RequestMethod.GET)
    public CommonResult<String> offShell(@PathVariable Integer id) {
        if (this.bannerService.hide(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 删除
     *
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:banner:delete')")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestBody @PathVariable Integer id) {
        if (this.bannerService.deleteBanner(id)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @PreAuthorize("hasAuthority('admin:jingangArea:list')")
    @RequestMapping(value = "/jingangArea/list", method = RequestMethod.GET)
    public CommonResult<List<JingangAreaVo>> getJingangAreaList() {
        return CommonResult.success(this.jingangAreaService.getJingangAreaList(false));
    }

    @PreAuthorize("hasAuthority('admin:jingangArea:save')")
    @RequestMapping(value = "/jingangArea/save", method = RequestMethod.POST)
    public CommonResult<Object> jingangAreaSave(@RequestBody @Validated JingangAreaVo param) {

        this.jingangAreaService.jingangAreaSave(param);
        return CommonResult.success();
    }

}
